import { get, post, IList } from "./index";

export interface Algorithm {
    createTime: string;
    id: number;
    name: string;
    share: number;
}

interface ListQuery {
    page: number;
    pageSize: number;
    isShare?: 1 | 2; // 1-共享 2-个人
    name?: string;
}

// 获取算法列表
export const getAlgorithms = async (data: ListQuery): Promise<IList<Algorithm>> => {
    const res = await post("/algorithm/private/list", data);
    // return res.data.algorithmPrivateDtoList || [];
    return { list: res.data.algorithmPrivateDtoList || [], total: res.data.total };
};

interface PublicParam {
    id: number;
    share: 1 | 2; // 是否发布 1-是 2-否
}

// 发布算法
export const publicAlgorithms = async (data: PublicParam) => post("/algorithm/publicShare", data);

// 创建算法
export const createAlgorithms = async (data: { name: string }) => post("/algorithm/create", data);

interface UpdateParam {
    id: number;
    name: string;
}
// 编辑算法
export const updateAlgorithms = async (data: UpdateParam) => post("/algorithm/update", data);

// 删除算法
export const delAlgorithms = async (id: number) => post("/algorithm/delete", { id });

interface DetailListQuery {
    algorithmId: number;
    page: number;
    pageSize: number;
    parentId: number;
}

export interface DetailItem {
    id: number;
    name: string;
    size: string;
    type: number; // 1-文件 2-文件夹
    updateTime: string;
}

type ListRespon = IList<DetailItem> & { filePath: string };
// 算法详情列表
export const getAlgorithmsDetailList = async (data: DetailListQuery): Promise<ListRespon> => {
    const res = await post("/algorithmfile/list", data);
    return {
        list: res.data.algorithmFileLDtoList || [],
        total: res.data.total || 0,
        filePath: res.data.filePath,
    };
};

interface AlgorithmsDetail {
    createTime: string;
    id: number;
    name: string;
    share: 1 | 2; // 是否发布 1-是 2-否
}

// 获取算法详情信息
export const getAlgorithmsDetail = async (algorithmId: number): Promise<AlgorithmsDetail> => {
    const res = await get("/algorithm/detail", { algorithmId });
    return res.data || {};
};

// 删除算法文件
export const delAlgorithmFiles = async (data: { algorithmId: number; fileIdList: number[] }) =>
    post("/algorithmfile/delete", data);

interface DownlodUrlParam {
    algorithmId: number;
    fileIdList: number[];
}
// 获取下载文件的url
export const getAlgorithmDownloadUrl = async (data: DownlodUrlParam): Promise<string> => {
    const res: any = await post("/algorithmfile/download", data, {
        responseType: "arraybuffer",
    });
    return res;
};

// 算法文件夹上传
export const uploadAlgorithmFileList = async (id: number, files: File[]) => {
    const formData = new FormData();
    formData.append("algorithmId", id);
    for (const file of files) {
        formData.append("folder", file);
    }
    const res = await post("/algorithmfile/uploadFolder", formData, {
        timeout: 20 * 60 * 1000,
        headers: {
            "Content-Type": "multipart/form-data",
        },
    });
    return res;
};
